package model;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CartDao {


    //插入商品到购物车
    public void insertShopToCart(List<Shop> cartShop, List<Integer> cartCount, int userId) {



        //与数据库建立连接
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            //1.建立连接   ?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/taobao?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false","root","dddd");


            //2.插入购物车商品前，要将购物车当中当前用户的商品删除再重新插入
            String deletesql = "delete from cart where userId = ?";
            statement = connection.prepareStatement(deletesql);
            statement.setInt(1,userId);
            statement.executeUpdate();
            //删除之前购物车商品成功
            System.out.println("删除之前购物车商品成功");

            System.out.println("开始插入购物车商品");
            //3.将shops中的数据全部插入orderdetail表中
            for (int i =0;i<cartShop.size();i++){
                String sql = "insert into cart values (?,?,?,?,?,?,?,?)";
                statement = connection.prepareStatement(sql);
                statement.setInt(1,userId);
                statement.setInt(2,cartShop.get(i).getShopId());
                statement.setString(3,cartShop.get(i).getCategory());
                statement.setString(4,cartShop.get(i).getIcon());
//                statement.setString(5,shops.get(i).getShopName());
                statement.setString(5,cartShop.get(i).getShopName());
                statement.setString(6,cartShop.get(i).getShopBrief());
                statement.setDouble(7,cartShop.get(i).getPrice());
                statement.setInt(8,cartCount.get(i));
                statement.executeUpdate();
                System.out.println("往购物车中插入第"+i+"个商品");
            }






        } catch (SQLException e) {
            e.printStackTrace();
        }







    }


    //获取对应userId 的订单
    public List<Shop> getAllShops(int userId) {
        List<Shop> shops = new ArrayList<>();

        //与数据库建立连接
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            //1.建立连接
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/taobao?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false","root","dddd");
            System.out.println("连接cart表成功");
            //2.查找数据
            String sql = "select * from cart where userId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,userId);
            resultSet = statement.executeQuery();
            //3.将查询到的数据放入shop对象，然后再放入shops集合
            while (resultSet.next()){
                Shop shop = new Shop();
                shop.setShopId(resultSet.getInt("shopId"));
                shop.setCategory(resultSet.getString("category"));
                shop.setIcon(resultSet.getString("icon"));
                shop.setShopName(resultSet.getString("shopName"));
                shop.setShopBrief(resultSet.getString("shopBrief"));
                shop.setPrice(resultSet.getDouble("price"));
                shops.add(shop);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }


        return shops;

    }

    //查找商品的数量
    public List<Integer> selectShopCount(int userId) {
        List<Integer> curnum = new ArrayList<>();

        //与数据库建立连接
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            //1.建立连接
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/taobao?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false","root","dddd");
            System.out.println("连接cart表成功");
            //2.查找数据
            String sql = "select count from cart where userId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,userId);
            resultSet = statement.executeQuery();
            //3.将查询到的数据放入shop对象，然后再放入shops集合
            while (resultSet.next()){
               Integer a = resultSet.getInt("count");
                curnum.add(a);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return curnum;
    }
}
